package com.aa.studentmanage.main;

import com.aa.studentmanage.entity.AdminEntity;
import com.aa.studentmanage.entity.LoginEntity;
import com.aa.studentmanage.entity.StudentEntity;
import com.aa.studentmanage.ui.AdminMenu;
import com.aa.studentmanage.ui.Menu;
import com.aa.studentmanage.utils.InputHelper;

public class AdminOperation {
    /*
     * 管理员登录
     * */
    public void adminLogin() {
        /*
         * 获取配置文件数据
         * */
        AdminEntity adminEntity = DataInt.adminEntity;
        for (int i = 1; i <= 3; i++) {
            //调用登录
            LoginEntity loginEntity = Menu.loginMenu("管理员");
            if (adminEntity.getAdminName().equals(loginEntity.getLoginName())
                    && adminEntity.getAdminPwd().equals(loginEntity.getLoginPwd())) {
                //调用管理员菜单
                adminMainHome();
                break;
            } else {
                if (i != 3) {
                    System.out.println("帐号或密码错误,还有" + (3 - i) + "次的登录机会!");
                } else {
                    System.out.println("错误次数过多，退出系统");
                    System.exit(0);
                }
            }
        }
    }

    /*
     * 管理员主菜单
     * */
    public void adminMainHome() {
        //菜单选项
        int adminNum = AdminMenu.adminMainMenu();
        if (adminNum >= 0 && adminNum <= 4) {
            switch (adminNum) {
                case 1: {
                    addStudent();
                    break;
                }
                case 2: {
                    modifyStudent();
                    break;
                }
                case 3: {
                    removeStudent();
                    break;
                }
                case 4: {
                    inquireStudent();
                    break;
                }
                case 0:
                    ProjectRun.chooseLogin();
            }
        } else {
            System.out.println("没有你要选择的菜单项,请重新选择:");
            adminMainHome(); // 递归调用
        }
    }

    /*
     * 新增学生
     * */
    private void addStudent() {
        System.out.println("***************** 新 增 学 生 *****************");
        StudentEntity studentEntity = AdminMenu.addStudentMenu();
        //存入数组
        DataInt.stuArrs[DataInt.arrIndex] = studentEntity;
        //数组下标加一
        DataInt.arrIndex++;
        System.out.println();
        System.out.println("新增成功!");
        while (true) {
            System.out.println("1、继续新增");
            System.out.println("0、返回上级");
            int Num = InputHelper.getInt();
            if (Num == 1) {
                addStudent();
                break;
            } else if (Num == 0) {
                System.out.println("正在返回上级");
                adminMainHome();
                break;
            } else {
                System.out.println("只能选择0-1,请重新选择:");
            }
        }
    }

    /*
     * 修改学生
     * */
    public void modifyStudent() {
        System.out.println("***************** 修 改 学 生 *****************");
        int chooseNum = AdminMenu.modifyStudentMenu();
        if (chooseNum >= 0 && chooseNum <= 3) {
            switch (chooseNum) {
                case 1:
                    modifyStudentById();
                    break;
                case 0:
                    System.out.println("正在返回上级菜单...");
                    adminMainHome();
            }
        }
    }

    //按学号修改
    public void modifyStudentById() {
        System.out.print("请输入要修改的学生编号:");
        int queryStuId = InputHelper.getInt();

        StudentEntity stuInfo = null;

        if (DataInt.stuArrs.length == 0) {
            System.out.println("暂无学生数据!");
        } else {
            for (int j = 0; j < DataInt.arrIndex; j++) {
                // 用数组里面的每个学生的编号和输入的学生编号进行比对
                if (DataInt.stuArrs[j].getStuId() == queryStuId) {
                    stuInfo = DataInt.stuArrs[j];
                    break;
                }
            }
        }
        if (stuInfo == null) {
            System.out.println("没有找到" + queryStuId + "编号的学生!");
        } else {
            AdminMenu.studentListTitle();
            System.out.printf("%-8s", stuInfo.getStuId() + "\t");
            System.out.printf("%-8s", stuInfo.getStuName() + "\t");
            System.out.printf("%-8s", stuInfo.getStuSex() + "\t");
            System.out.printf("%-8s", stuInfo.getStuAge() + "\t");
            System.out.printf("%-8s", stuInfo.getStuClasses() + "\t");
            System.out.printf("%-8s", stuInfo.getStuAddress() + "\t");
            System.out.println();
        }
        System.out.println("----------------------------------------------------");
        System.out.println("请输入修改的内容");
        System.out.print("请输入新的学生姓名:");
        stuInfo.setStuName(InputHelper.getString());
        System.out.print("请输入新的学生性别:");
        stuInfo.setStuSex(InputHelper.getString());
        System.out.print("请输入新的学生年龄:");
        stuInfo.setStuAge(InputHelper.getInt());
        System.out.print("请输入新的学生班级:");
        stuInfo.setStuClasses(InputHelper.getString());
        System.out.print("请输入新的学生住址:");
        stuInfo.setStuAddress(InputHelper.getString());
        System.out.println("修改完成");
        while (true) {
            System.out.println("1、继续修改");
            System.out.println("0、返回上级");
            int Num = InputHelper.getInt();
            if (Num == 1) {
                modifyStudent();
                break;
            } else if (Num == 0) {
                System.out.println("正在返回上级");
                adminMainHome();
                break;
            } else {
                System.out.println("只能选择0-1,请重新选择:");
            }
        }
    }

    /*
     * 删除学生
     * */
    public void removeStudent() {
        System.out.println("???");
    }

    /*
     * 查询学生
     * */
    public void inquireStudent() {
        int inquireNum = AdminMenu.inquireStudentMenu();
        if (inquireNum >= 0 && inquireNum <= 4) {
            switch (inquireNum) {
//                1、查询所有学生
                case 1:
                    inquireStudentAll();
                    break;
//                2、按照学号查询
                case 2:
                    inquireStudentStuId();
                    break;
                //3、按性别查询
                case 3:
                    inquireStudentBySex();
                    break;
                //4、按班级查询
                case 4:
                    inquireStudentByClasses();
                    break;
//                0、返回上级
                case 0: {
                    System.out.println();
                    System.out.println("正在返回上级");
                    adminMainHome();
                }
            }
            System.out.println("是否继续查询?(Y/N)");
            String YesorNo = InputHelper.getString();
            if (YesorNo == "Y" || YesorNo == "y") {
                inquireStudent();
            } else {
                adminMainHome();
            }
        } else {
            System.out.println("请选择正确的选项");
            inquireStudent();
        }
    }

    //查询所有学生
    public void inquireStudentAll() {
        if (DataInt.stuArrs.length == 0) {
            System.out.println("暂无学生数据!");
        } else {
            AdminMenu.studentListTitle();
            for (int j = 0; j < DataInt.arrIndex; j++) {
                // 获取每个学生的信息
                StudentEntity stuInfo = DataInt.stuArrs[j];
                System.out.printf("%-8s", stuInfo.getStuId() + "\t");
                System.out.printf("%-8s", stuInfo.getStuName() + "\t");
                System.out.printf("%-8s", stuInfo.getStuSex() + "\t");
                System.out.printf("%-8s", stuInfo.getStuAge() + "\t");
                System.out.printf("%-8s", stuInfo.getStuClasses() + "\t");
                System.out.printf("%-8s", stuInfo.getStuAddress() + "\t");
                System.out.println();
            }
        }
    }

    //按照学号查询
    public void inquireStudentStuId() {
        System.out.print("请输入要查询的学生编号:");
        int queryStuId = InputHelper.getInt();

        StudentEntity stuInfo = null;

        if (DataInt.stuArrs.length == 0) {
            System.out.println("暂无学生数据!");
        } else {
            for (int j = 0; j < DataInt.arrIndex; j++) {
                // 用数组里面的每个学生的编号和输入的学生编号进行比对
                if (DataInt.stuArrs[j].getStuId() == queryStuId) {
                    stuInfo = DataInt.stuArrs[j];
                    break;
                }
            }
        }

        if (stuInfo == null) {
            System.out.println("没有" + queryStuId + "编号的学生!");
        } else {
            AdminMenu.studentListTitle();
            System.out.printf("%-8s", stuInfo.getStuId() + "\t");
            System.out.printf("%-8s", stuInfo.getStuName() + "\t");
            System.out.printf("%-8s", stuInfo.getStuSex() + "\t");
            System.out.printf("%-8s", stuInfo.getStuAge() + "\t");
            System.out.printf("%-8s", stuInfo.getStuClasses() + "\t");
            System.out.printf("%-8s", stuInfo.getStuAddress() + "\t");
            System.out.println();
        }
    }

    //按性别查询
    public void inquireStudentBySex() {
        System.out.println("???");
    }

    //按班级查询
    public void inquireStudentByClasses() {
        System.out.println("???");
    }

}
